package datapro;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class GetStopRoute {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String dir = "C://mine/current/task13/google_transit/";

		// load stop
		BufferedReader reader = new BufferedReader(new FileReader(dir
				+ "stops.txt"));
		String line;
		HashMap<String,Stop> stopHash = new HashMap<String,Stop>();
		reader.readLine();
		while ((line = reader.readLine()) != null) {
			if (line.length() > 0) {
				line = line.trim();
				String[] tmp = line.split(",");
				Stop ts = new Stop();
				ts.setId(tmp[0].trim());
				ts.setName(tmp[2].trim());
				ts.setLat(tmp[4].trim());
				ts.setLon(tmp[5].trim());
				stopHash.put(ts.getId(), ts);
			}
		}
		reader.close();
		// join trip with route
		HashMap<String,List<String>> trip2route=new HashMap<String,List<String>>();
		reader = new BufferedReader(new FileReader(dir + "trips.txt"));
		reader.readLine();
		while ((line = reader.readLine()) != null) {
			if (line.length() > 0) {
				line = line.trim();
				String[] tmp = line.split(",");
				String tripId=tmp[2].trim();
				String routeId=tmp[0].trim();
				if(!trip2route.containsKey(tripId)){
					List<String> tmp1=new ArrayList<String>();
					tmp1.add(routeId);
					trip2route.put(tripId, tmp1);
				}
				else{
					trip2route.get(tripId).add(routeId);
				}
			}
		}
		reader.close();
		// join stop with trip
		reader = new BufferedReader(new FileReader(dir + "stop_times.txt"));
		reader.readLine();
		while ((line = reader.readLine()) != null) {
			if (line.length() > 0) {
				line = line.trim();
				String[] tmp = line.split(",");
				String tripId=tmp[0].trim();
				String stopId=tmp[3].trim();
				List<String> tmp2=trip2route.get(tripId);
				stopHash.get(stopId).addRoutes(tmp2);
			}
		}
		reader.close();
		//release to save memory
		trip2route=null;
		
		//mapping from route id to name
		HashMap<String,String> routeHash=new HashMap<String,String>();
		reader = new BufferedReader(new FileReader(dir + "routes.txt"));
		reader.readLine();
		while ((line = reader.readLine()) != null) {
			if (line.length() > 0) {
				line = line.trim();
				String[] tmp = line.split(",");
				String routeId=tmp[0].trim();
				String routeName0=tmp[1].trim();
				routeHash.put(routeId, routeName0);
			}
		}
		reader.close();
		
		//print to file
		BufferedWriter writer=new BufferedWriter(new FileWriter(dir+"stopsv1.txt"));
		Set<String> keys=stopHash.keySet();
		for(String key:keys){
			Stop tmpStop=stopHash.get(key);
			String str=tmpStop.toString();
			for(String routeId:tmpStop.getRoutes()){
				String routeName1=routeHash.get(routeId);
				str+=","+routeName1;
			}
			writer.write(str);
			writer.newLine();
		}
		writer.close();
	}

}
